package com.classs05.c_other;

import java.util.Comparator;

public class Student implements Comparable ,Comparator{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    //    this:代表student[i]
//    o:代表student[i+1]
//    如果this.age > s.age,返回正整数 证明数组中前一个学生年龄大于后一个学生年龄
//
    @Override
    public int compareTo(Object o) {
        Student s = (Student) o;
        return this.age - s.age;
    }

    /**
     *
     * @param o1 the first object to be compared.
     * @param o2 the second object to be compared.
     * @return
     * o1代表student[i]
     * o2代表student[i+1]
     * 如果o1.age > o2.age,返回正整数 证明数组中前一个学生年龄大于后一个学生年龄
     * 如果o1.age < o2.age,返回负整数 证明数组中前一个学生年龄小于后一个学生年龄
     * 如果o1.age == o2.age,返回0 证明数组中前一个学生年龄等于后一个学生年龄
     */

    @Override
    public int compare(Object o1, Object o2) {
       Student s1 = (Student) o1;
       Student s2 = (Student) o2;
       return s1.age - s2.age;
    }
}
